package cn.iocoder.yudao.module.nmkj.service.introduction;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.module.nmkj.controller.admin.introduction.vo.*;
import cn.iocoder.yudao.module.nmkj.dal.dataobject.introduction.IntroductionDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.nmkj.dal.mysql.introduction.IntroductionMapper;

import java.util.Collections;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.INTRODUCTION_NOT_EXISTS;

/**
 * 商品介绍 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class IntroductionServiceImpl implements IntroductionService {

    @Resource
    private IntroductionMapper introductionMapper;

    @Override
    public Long createIntroduction(IntroductionSaveReqVO createReqVO) {
        // 插入
        IntroductionDO introduction = BeanUtils.toBean(createReqVO, IntroductionDO.class);
        introductionMapper.insert(introduction);
        // 返回
        return introduction.getId();
    }

    @Override
    public void updateIntroduction(IntroductionSaveReqVO updateReqVO) {
        // 校验存在
        validateIntroductionExists(updateReqVO.getId());
        // 更新
        IntroductionDO updateObj = BeanUtils.toBean(updateReqVO, IntroductionDO.class);
        introductionMapper.updateById(updateObj);
    }

    @Override
    public void deleteIntroduction(Long id) {
        // 校验存在
        validateIntroductionExists(id);
        // 删除
        introductionMapper.deleteById(id);
    }

    private void validateIntroductionExists(Long id) {
        if (introductionMapper.selectById(id) == null) {
            throw exception(INTRODUCTION_NOT_EXISTS);
        }
    }

    @Override
    public IntroductionDO getIntroduction(Long id) {
        return introductionMapper.selectById(id);
    }

    @Override
    public PageResult<IntroductionDO> getIntroductionPage(IntroductionPageReqVO pageReqVO) {
        return introductionMapper.selectPage(pageReqVO);
    }

    @Override
    public List<IntroductionDO> getIntroductionList(IntroductionPageReqVO reqVO) {
        return introductionMapper.selectList(reqVO);
    }

}